﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ChatLieu : Form
    {
        
        public ChatLieu()
        {
            InitializeComponent();
            
        }

        private void ChatLieu_Load(object sender, EventArgs e)
        {
            LoadDataGridView1();
        }

        private void LoadDataGridView1()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChatLieu";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();

            DataSet ds = new DataSet();
            adapter.Fill(ds, "ChatLieu");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ds.Tables["ChatLieu"];
            conn.Close();
        }

        

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Them(txtMaSo_CL.Text,txtTen_CL.Text);
            LoadDataGridView1();
        }
        private void Them(string MaSo_CL,string Ten_CL)
        {
            if (txtMaSo_CL.Text.Length == 0 || txtTen_CL.Text.Length == 0)
                TBTCong.Text = "Thêm mới chất liệu không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "insert into ChatLieu(MaSo_CL,Ten_CL) values (@MaSo_CL,@Ten_CL)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_CL", MaSo_CL);
                cmd.Parameters.AddWithValue("@Ten_CL", Ten_CL);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCong.Text = "Thêm mới chất liệu thành công";
            }
            
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            CapNhat();
            LoadDataGridView1();
        }
        private void CapNhat()
        {
            if (txtMaSo_CL.Text.Length == 0 || txtTen_CL.Text.Length == 0)
                TBTCong.Text = "Cập nhật chất liệu không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "update ChatLieu set Ten_CL=@Ten_CL where MaSo_CL=@MaSo_CL";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_CL", txtMaSo_CL.Text);
                cmd.Parameters.AddWithValue("@Ten_CL", txtTen_CL.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCong.Text = "Cập nhật chất liệu thành công";
            }
        }

        
        private void buttonDel_Click(object sender, EventArgs e)
        {
            Delete();
            LoadDataGridView1();
        }
        private void Delete()
        {
            if (txtMaSo_CL.Text.Length == 0 || txtTen_CL.Text.Length == 0)
                TBTCong.Text = "Xóa chất liệu không thành công";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from ChatLieu where MaSo_CL = @MaSo_CL";
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.Parameters.AddWithValue("@MaSo_CL", txtMaSo_CL.Text);
                cmd.Parameters.AddWithValue("@Ten_CL", txtTen_CL.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                TBTCong.Text = "Xóa chất liệu thành công";
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSCL;
            MSCL = dataGridView1.Rows[e.RowIndex].Cells["MaSo_CL"].Value.ToString();
            loadCL(MSCL);
        }
        private void loadCL(string MSCL)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from ChatLieu where MaSo_CL = @MaSo_CL";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_CL", MSCL);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                txtMaSo_CL.Text = reader["MaSo_CL"].ToString();
                txtTen_CL.Text = reader["Ten_CL"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void butClearCL_Click(object sender, EventArgs e)
        {
            txtMaSo_CL.Text = null;
            txtTen_CL.Text = null;
            TBTCong.Text = null;
        }
        



       



       
       


       



       

        
    }
}
